package com.maidshop.animation.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.maidshop.animation.domain.Animation;
import com.maidshop.animation.domain.dto.AnimationDto;
import com.maidshop.animation.domain.dto.FileDto;
import com.maidshop.animation.domain.vo.AnimationVo;
import com.maidshop.animation.mapper.AnimationMapper;
import com.maidshop.animation.service.AnimationService;
import com.maidshop.elasticsearch.domain.AnimationEs;
import com.maidshop.elasticsearch.service.ElasticsearchService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 22384
 * @description 针对表【a_animation】的数据库操作Service实现
 * @createDate 2024-10-20 20:51:10
 */
@Service
@Transactional(readOnly = true)
public class AnimationServiceImpl extends ServiceImpl<AnimationMapper, Animation>
        implements AnimationService {

    @Resource
    private AnimationMapper animationMapper;

    @Resource
    private ElasticsearchService elasticsearchService;

    /**
     * 查询所有动画
     *
     * @param vo
     * @return
     */
    @Override
    public List<AnimationDto> selectList(AnimationVo vo) {
        return animationMapper.listAll(vo);
    }

    /**
     * 新增动画
     *
     * @param animation
     * @return
     */
    @Transactional
    public int insertNewAnimation(Animation animation) {
        int i = animationMapper.insertNewAnimation(animation);
        int j = 0;
        if (StringUtils.isNotBlank(animation.getCover())) {
            j = animationMapper.insertCover(animation.getId().toString(), animation.getCover());
        }
        // 同步es数据
        AnimationEs es = new AnimationEs();
        // 单独设置id(类型不一致)(已修改)
        es.setId(animation.getId());
        BeanUtils.copyProperties(animation, es);
        // 当实际图片地址不为空时，设置封面
        if (StringUtils.isNotBlank(animation.getActualUrl())) {
            es.setCover(animation.getActualUrl());
        }
        elasticsearchService.save(es);
        return i + j;
    }

    /**
     * 修改动画
     *
     * @param animation
     * @return
     */
    @Transactional
    public int updateAnimation(Animation animation) {
        if (StringUtils.isNotBlank(animation.getCover()) && animation.getFileId() == null) {
            // 此时为之前没有封面，现在添加新的封面
            // 再插入中间表信息
            animationMapper.insertCover(animation.getId().toString(), animation.getCover());
        } else {
            // 正常修改封面
            // 先删除中间表信息
            animationMapper.deleteCover(animation.getId());
            // 再插入中间表信息
            animationMapper.insertCover(animation.getId().toString(), animation.getFileId());
        }

        // 同步es数据
        AnimationEs es = new AnimationEs();
        BeanUtils.copyProperties(animation, es);
        if (StringUtils.isNotBlank(animation.getActualUrl())) {
            // 未加图片时保证第一次更新同步es
            es.setCover(animation.getActualUrl());
        }
        elasticsearchService.updateAnimationEs(es);

        return animationMapper.updateAnimation(animation);
    }

    /**
     * 获取单个动画详情
     *
     * @param id
     * @return
     */
    public AnimationDto selectOne(Long id) {
        AnimationDto animationDto = animationMapper.selectOneAnimation(id);
        // 单独查询封面
        FileDto dto = animationMapper.selectCover(id);
        if (dto!= null) {
            animationDto.setFileId(dto.getFileId());
            animationDto.setCover(dto.getFilePath());
        }
        // 初始化es数据
//        elasticsearchService.initElasticsearch();
        return animationDto;
    }

    /**
     * 删除动画
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public int deleteAnimation(Long id) {
        int i = animationMapper.deleteAnimation(id);
        // 删除中间表信息
        int j = animationMapper.deleteCover(id);
        // 删除es数据
        String string = id.toString();
        elasticsearchService.deleteById(id);
        return i + j;
    }

    @Override
    public List<AnimationEs> findAllAnimationEs() {
        return animationMapper.findAllAnimationEs();
    }
}




